package com.example.auth.demo.domain.auth;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

public class UserDetail implements UserDetails {
    private long id;
    private String username;
    private String password;
    private Role role;
    private Date lastPasswordResetDate;
    private String nickname; // 新增的 nickname 字段

    // 新增的构造函数
    public UserDetail(long id, String username, String password, String nickname) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.role = new Role(); // 设置默认角色
        this.role.setName("ROLE_DEFAULT");
    }

    // 其他构造函数
    public UserDetail(
            long id,
            String username,
            Role role,
            String password) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.role = role != null ? role : new Role();
        this.role.setName("ROLE_DEFAULT");
        this.nickname = "default"; // 可选：设置默认值
    }

    public UserDetail(String username, String password, Role role) {
        this.username = username;
        this.password = password;
        this.role = role != null ? role : new Role();
        this.role.setName("ROLE_DEFAULT");
        this.nickname = "default"; // 可选：设置默认值
    }

    public UserDetail(long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.nickname = "default"; // 可选：设置默认值
        this.role = new Role(); // 设置默认角色
        this.role.setName("ROLE_DEFAULT");
    }

    // 返回分配给用户的角色列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (this.role != null && this.role.getName() != null) {
            authorities.add(new SimpleGrantedAuthority(this.role.getName()));
        } else {
            authorities.add(new SimpleGrantedAuthority("ROLE_DEFAULT"));
        }
        return authorities;
    }

    public long getId() {
        return id;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    /**
     * 账户是否未过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否未锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否未过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否激活
     */
    @Override
    public boolean isEnabled() {
        return true;
    }

    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setLastPasswordResetDate(Date lastPasswordResetDate) {
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    // 👇 新增的 getter 和 setter
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}